<template>
  <!-- 文章分类详情抽屉 -->  
  <div class="app-appNewsCategory-detail">
    <el-drawer v-model="isShowDialog" size="80%" direction="ltr">
      <template #header>
        <h4>文章分类详情</h4>
      </template>
      <el-form ref="formRef" :model="formData" label-width="100px">          
        <el-row>        
          <el-col :span="12">          
            <el-form-item label="ID">{{ formData.id }}</el-form-item>          
          </el-col>        
          <el-col :span="12">          
            <el-form-item label="名称">{{ formData.cateName }}</el-form-item>          
          </el-col>        
          <el-col :span="12">          
            <el-form-item label="父ID">{{ formData.pid }}</el-form-item>          
          </el-col>      
        </el-row>      
      </el-form>
    </el-drawer>
  </div>
</template>
<script lang="ts">
  import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance,computed } from 'vue';
  import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';  
  import {
    listAppNewsCategory,
    getAppNewsCategory,
    delAppNewsCategory,
    addAppNewsCategory,
    updateAppNewsCategory,    
  } from "/@/api/app/appNewsCategory";  
  import {
    AppNewsCategoryTableColumns,
    AppNewsCategoryInfoData,
    AppNewsCategoryTableDataState,
    AppNewsCategoryEditState
  } from "/@/views/app/appNewsCategory/list/component/model"
  export default defineComponent({
    name:"ApiV1AppAppNewsCategoryDetail",    
    props:{      
      // 文章分类树选项
      appNewsCategoryOptions :{
        type:Array,
        default:()=>[]
      },      
    },    
    setup(props,{emit}) {      
      const {proxy} = <any>getCurrentInstance()
      const formRef = ref<HTMLElement | null>(null);
      const menuRef = ref();      
      const state = reactive<AppNewsCategoryEditState>({
        loading:false,
        isShowDialog: false,
        formData: {          
          id: undefined,          
          cateName: undefined,          
          pid: undefined,          
        },
        // 表单校验
        rules: {          
          id : [
              { required: true, message: "ID不能为空", trigger: "blur" }
          ],          
          cateName : [
              { required: true, message: "名称不能为空", trigger: "blur" }
          ],          
        }
      });
        // 打开弹窗
        const openDialog = (row?: AppNewsCategoryInfoData) => {
          resetForm();
          if(row) {
            getAppNewsCategory(row.id!).then((res:any)=>{
              const data = res.data;              
              data.pid = getTreeVal(props.appNewsCategoryOptions,data.pid)              
              state.formData = data;
            })
          }
          state.isShowDialog = true;
        };
        // 关闭弹窗
        const closeDialog = () => {
          state.isShowDialog = false;
        };
        // 取消
        const onCancel = () => {
          closeDialog();
        };
        const resetForm = ()=>{
          state.formData = {            
            id: undefined,            
            cateName: undefined,            
            pid: undefined,            
          }
        };        
        const getTreeVal = (data : any[],val : any) : any=>{
          let findV:any = null
          data.some((item:any)=>{
            if(item.id==val){
              findV = item.cateName
              return true
            }
            if(item.children) {
              findV = getTreeVal(item.children, val)
              if (findV) {
                return findV
              }
            }
          })
          return findV
        }        
        return {
          proxy,
          openDialog,
          closeDialog,
          onCancel,
          menuRef,
          formRef,          
          ...toRefs(state),
        };
      }
  })
</script>
<style scoped>  
  .app-appNewsCategory-detail :deep(.el-form-item--large .el-form-item__label){
    font-weight: bolder;
  }
  .pic-block{
    margin-right: 8px;
  }
  .file-block{
    width: 100%;
    border: 1px solid var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
    margin-bottom: 5px;
    padding: 3px 6px;
  }
  .ml-2{margin-right: 5px;}
</style>